﻿using Corina.Enumerations;
using Corina.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Corina.Datamodel.Base
{
    [DataContract]
    public class QueryCondition : NotificationObject
    {
        public QueryCondition() { }

        public QueryCondition(string parameter, params object[] paras)
        {
            Parameter = parameter;
            if (paras.Count() == 1)
            {
                ValueStart = paras[0];
            }
            if (paras.Count() > 1)
            {
                ValueStart = paras[0];
                ValueEnd = paras[1];
            }
        }

        #region Fields
        private string _parameter;
        private object _valueStart;
        private object _valueEnd;
        #endregion

        [DataMember]
        public string Parameter {
            get { return _parameter; }
            set
            {
                _parameter = value;
                RaisePropertyChanged(() => Parameter);
                RaisePropertyChanged(() => IsValid);
            }
        }

        [DataMember]
        public object ValueStart {
            get { return _valueStart; }
            set
            {
                _valueStart = value;
                RaisePropertyChanged(() => ValueStart);
                RaisePropertyChanged(() => IsValid);
                RaisePropertyChanged(() => Symbol);
            }
        }

        [DataMember]
        public object ValueEnd {
            get { return _valueEnd; }
            set
            {
                _valueEnd = value;
                RaisePropertyChanged(() => ValueEnd);
                RaisePropertyChanged(() => IsValid);
                RaisePropertyChanged(() => Symbol);
            }
        }

        public bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(Parameter) && (ValueStart != null || ValueEnd != null);
            }
        }

        public ConditionValueSymbol Symbol
        {
            get
            {
                if (ValueStart == null && ValueEnd != null)
                {
                    return ConditionValueSymbol.Smaller;
                }
                if (ValueStart != null && ValueEnd == null)
                {
                    return ConditionValueSymbol.Greater;
                }
                if (ValueStart == ValueEnd)
                {
                    return ConditionValueSymbol.Equals;
                }
                if (((IComparable)ValueStart).CompareTo((IComparable)ValueEnd) == 0)
                {
                    return ConditionValueSymbol.Equals;
                }
                if (((IComparable)ValueStart).CompareTo((IComparable)ValueEnd) < 0)
                {
                    return ConditionValueSymbol.Between;
                }
                if (((IComparable)ValueStart).CompareTo((IComparable)ValueEnd) > 0)
                {
                    return ConditionValueSymbol.NotEquals;
                }
                else
                {
                    return ConditionValueSymbol.NotEquals;
                }
            }
        }
    }
}
